package com.rocky.server.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rocky.server.service.UserService;

public class RegisterServlet extends HttpServlet {

	private static final long serialVersionUID = -5779101580336426137L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String username = "";
		String password = "";
		String nickname = "";
		String birthday = "";
		int sex = 0;
		req.setCharacterEncoding("utf-8");
		if(req.getParameter("username")!=null && !req.getParameter("username").equals("")){
			username = req.getParameter("username");
		}
		if(req.getParameter("password")!=null && !req.getParameter("password").equals("")){
			password = req.getParameter("password");
		}
		if(req.getParameter("nickname")!=null && !req.getParameter("nickname").equals("")){
			nickname = req.getParameter("nickname");
		}
		if(req.getParameter("birthday")!=null && !req.getParameter("birthday").equals("")){
			birthday = req.getParameter("birthday");
		}
		if(req.getParameter("sex")!=null && !req.getParameter("sex").equals("")){
			sex = Integer.valueOf(req.getParameter("sex")).intValue();
		}
		
		UserService us = new UserService();
		boolean flag = us.registerUser(username, password, nickname, birthday, sex);
		resp.setCharacterEncoding("utf-8");
		PrintWriter out = resp.getWriter();
		if(flag){
			out.print("SUCCESS");
		}else{
			out.print("FAIL");
		}
		out.flush();
		out.close();
	}
	
	

}
